#!/bin/bash
#
#这个脚本是用来统计svn项目的代码
#
db_host='127.0.0.1'
db_port='3306'
db_user='root'
db_pswd='qwe123'
db_name='svncal'
program_name='program_1'
#program_path='/home/workplace/svnfile'
program_path='/mnt/ZAFK'

#数据库中已统计过的版本
db_count=`mysql -h $db_host -D $db_name -P $db_port -u $db_user -p$db_pswd -e "select count(*) from $program_name"`
ver_num=`echo $db_count | awk '{print $2}'`  
ver_num=$((${ver_num}+1))

#已更新到的版本
cd $program_path
ver_num1=`svn info | sed -n 6p | cut -d " " -f 2`

#对两个版本进行验证


#对每个版本进行统计

for ver in `seq $ver_num $ver_num1`;do
    echo $ver
    svn_user=`svn log -c $ver | head -2 | tail -1| awk 'BEGIN{FS="|"}{print $2}'`
    svn_date=`svn log -c $ver | head -2 | tail -1| awk 'BEGIN{FS="|"}{print $3}'|cut -d "+" -f 1`
#    svn_stat=`svn log -c $ver | tail -2 | head -1`
    svn_file=`svn log -v -c $ver | grep ^[[:space:]] |wc -l `
    svn_add=`svn diff -c $ver | grep -v +++ | grep ^+ | wc -l`
    svn_del=`svn diff -c $ver | grep -v '\---' | grep ^- | wc -l`
#    echo "$svn_date"
#    echo "$svn_user,$svn_date,$svn_stat,$svn_file,$svn_add,$svn_del"
    mysql -h $db_host -D $db_name -P $db_port -u $db_user -p$db_pswd <<EOF
	insert into $program_name values ( $ver , "$svn_date" , "$svn_user" , $svn_file , $svn_add , $svn_del)
EOF
done
